/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2.config.bean;

import com.vimhe.ezimp2.config.exception.ConfigException;
import com.vimhe.ezimp2.util.ConvertUtils;
import com.vimhe.ezimp2.util.StringUtils;

import java.util.Collections;
import java.util.Map;

/**
 * 行配置对象
 *
 * @author Vimhe
 */
public final class RowConfig {

    /**
     * 从哪一行开始读取
     */
    private Integer start;

    /**
     * 读取到哪一行结束
     */
    private Integer end;

    /**
     * 单元格配置对象
     */
    private Map<String, ColumnConfig> columnConfigMap = Collections.emptyMap();

    public Integer getStart() {
        return start;
    }

    public void setStart(String start) {
        if (StringUtils.isBlank(start)) {
            start = "1";
        }

        try {
            Integer i = ConvertUtils.convert(start, Integer.class);
            this.setStart(i);
        } catch (Exception e) {
            throw new ConfigException("配置项【row > start】的值【" + start + "】是无效的，该值只能是整数");
        }
    }

    public void setStart(Integer start) {
        if (start < 1 || start > 1048576) {
            throw new ConfigException("配置项【row > start】的值【" + start + "】是无效的，该值必须是1-1048576间的整数");
        }

        this.start = start;
    }

    public Integer getEnd() {
        return end;
    }

    public void setEnd(String end) {
        if (StringUtils.isBlank(end)) {
            end = "1048576";
        }

        try {
            Integer i = ConvertUtils.convert(end, Integer.class);
            this.setEnd(i);
        } catch (Exception e) {
            throw new ConfigException("配置项【row > end】的值只能是整数");
        }
    }

    public void setEnd(Integer end) {
        if (end < 1 || end > 1048576) {
            throw new ConfigException("配置项【row > end】的值【" + end + "】是无效的，该值必须是1-1048576间的整数");
        }

        if (end < this.start) {
            throw new ConfigException("配置项【row > end】的值【" + end
                    + "】是无效的，该值必须大于或等于配置项【row > start】的值【" + this.start + "】");
        }

        this.end = end;
    }

    public Map<String, ColumnConfig> getColumnConfigMap() {
        return columnConfigMap;
    }

    public void setColumnConfigMap(Map<String, ColumnConfig> columnConfigMap) {
        this.columnConfigMap = columnConfigMap;
    }

}
